﻿using Authentication.Model.Interface;
using Authentication.Model.Model;
using Common.Contract;
using Common.Contract.Authentication;
using Common.Contract.Authentication.Identifier;
using Common.Contract.Authentication.Request;
using Common.Service.Command;
using Common.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Authentication.ServiceCommand
{
    public class AddObjectServiceCommand : ProviderServiceCommand<IObjectProvider, AddObjectRequest, ResponseRecord>
    {
        public AddObjectServiceCommand(IObjectProvider providers) : base(providers) { }
        protected override void Validate(AddObjectRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (request.Record == null)
            {
                throw new ArgumentException("Record must be set", "request");
            }
            //if (!RoleIdentifier.IsValid(request.Record.ObjectIdentifier))
            //{
            //    throw new ArgumentException("RoleIdentifier is invalidate", "request");
            //}
        }

        protected override bool OnExecute(ResponseRecord response)
        {
            var applicationKey = Request.Header.ApplicationKey;
            var result = false;
            if (Request.Record.IsSubstitution != true)
            {
                var obj = new ApplicationObject
                            {
                                Active = BooleanString.TRUE,
                                ApplicationKey = applicationKey.ToString().ToUpper(),
                                Id = Request.Record.ObjectIdentifier,
                                ObjectIconUrl = Request.Record.ObjectIcon,
                                ObjectNameEnglish = Request.Record.ObjectName,
                                ObjectNameLocal = Request.Record.ObjectName,
                                ObjectUrl = Request.Record.ObjectUrl,
                                ParentId = Request.Record.ParentIdentifier
                            };

                result = Providers.Add(obj);
            }
            else
            {
                var obj = new ObjectSubstitution
                {
                    Active = BooleanString.TRUE,
                    ApplicationKey = applicationKey.ToString().ToUpper(),
                    Id = Request.Record.ObjectIdentifier, 
                    ObjectNameEnglish = Request.Record.ObjectName,
                    ObjectNameLocal = Request.Record.ObjectName, 
                    ParentId = Request.Record.ParentIdentifier
                };

                result = Providers.Add(obj);
            }

            return result;
        }
    }
}
